import {
  GetConfigForNetAuth, GetConfigForCpu, GetConfigForOldRdp, GetConfigForStartType, GetRdpPort, GetDisabledDiskStatus, GetHideDiskStatus,
  SetConfigForCpu, SetConfigForNetAuth, SetConfigForOldRdp, SetConfigForStartType, SetRdpPort, SetDisabledDiskStatus, SetHideDiskStatus,
} from "../../../wailsjs/go/view/App"
import { ElMessage } from 'element-plus'

export interface oldInfo {
  rdpPort: number
  startForPath: boolean
  multiSession: boolean
  noNetworkAuth: boolean
  avgCPU: boolean
  oldRdp: boolean
  hideDisk: boolean,
  disabledDisk: boolean,
  hideNavigation: boolean,
  disabledRightClick: boolean,
}
// 加载现有配置
const loadConfig = async (): Promise<oldInfo> => {
  // GetRdpPort
  const info: oldInfo = {
    rdpPort: 0,
    startForPath: false,
    multiSession: false, // 多会话还未开启
    noNetworkAuth: false,
    avgCPU: false,
    oldRdp: false,
    hideDisk: false,
    disabledDisk: false,
    hideNavigation: false,
    disabledRightClick: false,
  }
  try {
    info.rdpPort = await GetRdpPort()
    info.noNetworkAuth = await GetConfigForNetAuth()
    info.avgCPU = await GetConfigForCpu()
    info.oldRdp = await GetConfigForOldRdp()
    info.startForPath = await GetConfigForStartType()
    info.disabledDisk = await GetDisabledDiskStatus()
    info.hideDisk = await GetHideDiskStatus()
  } catch (err: unknown) {
    ElMessage({
      message: err!.toString(),
      type: 'warning',
    })
  }
  return info
}

interface checkBoxList {
  rdpPort: boolean
  startForPath: boolean
  multiSession: boolean
  noNetworkAuth: boolean
  avgCPU: boolean
  oldRdp: boolean
  hideDisk: boolean
  disabledDisk: boolean
}

// 设置新配置

const setConfig = async (old: oldInfo, cbl: checkBoxList, rdpPort: number): Promise<void> => {
  try {
    if (cbl.rdpPort) {
      // 如果勾选了rdp选择框，并且新旧 值不同就调用更新
      SetRdpPort(rdpPort.toString())
    }
    // 其他的勾选框如果和旧数据不一致才调用更新
    if (old.startForPath != cbl.startForPath) {
      SetConfigForStartType(cbl.startForPath)
    }
    if (old.noNetworkAuth != cbl.noNetworkAuth) {
      SetConfigForNetAuth(cbl.noNetworkAuth)
    }
    if (old.avgCPU != cbl.avgCPU) {
      SetConfigForCpu(cbl.avgCPU)
    }
    if (old.oldRdp != cbl.oldRdp) {
      SetConfigForOldRdp(cbl.oldRdp)
    }
    if (old.hideDisk != cbl.hideDisk) {
      SetHideDiskStatus(cbl.hideDisk)
    }
    if (old.disabledDisk != cbl.disabledDisk) {
      SetDisabledDiskStatus(cbl.disabledDisk)
    }
  } catch (err: unknown) {
    ElMessage({
      message: err!.toString(),
      type: 'warning',
    })
  }
  ElMessage({
    message: "策略执行成功，部分策略需要重启后才能生效！",
    type: 'success',
  })
}


export { loadConfig, setConfig }

